<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>

        // 链表
        // const wmz = {
        //     name: 'wangmazi',
        //     next: null,
        // };

        // const ls = {
        //     name: 'lisi',
        //     next: wmz,
        // };

        // const zs = {
        //     name: 'zhangsan',
        //     next: ls,
        // };

        // CDN
        // 源站 北京
        // 西南片区 武汉
        //  武汉 >>> 重庆 成都 ...
        // 成都 >>> 金牛 武侯 ...

        const arr1 = [1, 2, 3, 99, 100];
        const arr2 = [0, 1, 2, 4, 11];

        const fn1 = (arr1, arr2) => [ ...arr1, ...arr2 ].sort((a, b) => a - b);
        function fn2 (arr1, arr2) {
            const result = []; // [0, 1, 1]
            let j = 0; // 2
            const length2 = arr2.length;

            for (let i = 0; i < arr1.length; i++) {
                while (arr1[i] >= arr2[j]) { 
                    result.push(arr2[j]);
                    j++;
                }
                result.push(arr1[i]);
            }
            if (j < length2) {
                for (let i = j; i < length2; i++) {
                    result.push(arr2[i]);
                }
            }
            // ...实则是循环
            // slice实则是循环+新内存
            return result;
        }

        function performance (name, cb, count = 10000) {
            console.time(name);
            for (let i = 0; i < count; i++) {
                cb();
            }
            console.timeEnd(name);
        }

        const createSortedArr = () => new Array(100).fill(0).map(() => Math.floor(Math.random() * 10000)).sort((a, b) => a - b);

        performance('暴力', () => {
            const arr1 = createSortedArr();
            const arr2 = createSortedArr();
            fn1(arr1, arr2);
        }, 10000);

        performance('优雅', () => {
            const arr1 = createSortedArr();
            const arr2 = createSortedArr();
            fn2(arr1, arr2);
        }, 10000);


    </script>
</body>
</html>